package com.gohome.gohomechat.ui.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.gohome.gohomechat.GHomeModel;
import com.gohome.gohomechat.R;

import com.gohome.gohomechat.ui.activity.LoginActivity;
import com.gohome.gohomechat.view.EaseSwitchButton;

/**
 * Created by dllo on 16/3/1.
 */
public class SetFragment extends BaseFragment implements View.OnClickListener {
    private Context context;
    private EaseSwitchButton btn_remind, btn_voice, btn_vibration, btn_outside, btn_allow;
    private RelativeLayout relative_voice, relative_vibration;
    private TextView text_voice, text_vibration;
    private Button btn_logout;
    private EMChatOptions chatOptions;
    private GHomeModel settingModel;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    protected int getLayout() {
        return R.layout.fragment_set;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initView(View view) {
        btn_remind = (EaseSwitchButton) view.findViewById(R.id.btn_remind);
        btn_voice = (EaseSwitchButton) view.findViewById(R.id.btn_voice);
        btn_vibration = (EaseSwitchButton) view.findViewById(R.id.btn_vibration);
        btn_outside = (EaseSwitchButton) view.findViewById(R.id.btn_outside);
        btn_allow = (EaseSwitchButton) view.findViewById(R.id.btn_allow);
        relative_vibration = (RelativeLayout) view.findViewById(R.id.vibration_relative);
        relative_voice = (RelativeLayout) view.findViewById(R.id.voice_relative);
        text_voice = (TextView) view.findViewById(R.id.text_voice);
        text_vibration = (TextView) view.findViewById(R.id.text_vibration);
        btn_logout = (Button) view.findViewById(R.id.btn_logout);
        btn_remind.setOnClickListener(this);
        btn_voice.setOnClickListener(this);
        btn_vibration.setOnClickListener(this);
        btn_outside.setOnClickListener(this);
        btn_allow.setOnClickListener(this);
        btn_logout.setOnClickListener(this);

        settingModel = new GHomeModel(context);
        if (settingModel.getSettingMsgNotification()) {
            btn_remind.openSwitch();
        } else {
            btn_remind.closeSwitch();
        }

        // 是否打开声音
        if (settingModel.getSettingMsgSound()) {
            btn_voice.openSwitch();
        } else {
            btn_voice.closeSwitch();
        }
        // 震动
        if (settingModel.getSettingMsgVibrate()) {
            btn_vibration.openSwitch();
        } else {
            btn_vibration.closeSwitch();
        }

        // 扬声器
        if (settingModel.getSettingMsgSpeaker()) {
            btn_outside.openSwitch();
        } else {
            btn_outside.closeSwitch();
        }


    }

    @Override
    protected void initData() {
        chatOptions = EMChatManager.getInstance().getChatOptions();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_remind:
                if (btn_remind.isSwitchOpen()) {
                    btn_remind.closeSwitch();
                    relative_voice.setVisibility(View.GONE);
                    relative_vibration.setVisibility(View.GONE);
                    text_voice.setVisibility(View.GONE);
                    text_vibration.setVisibility(View.GONE);
                    chatOptions.setNotifyBySoundAndVibrate(false);
                } else {
                    btn_remind.openSwitch();
                    relative_voice.setVisibility(View.VISIBLE);
                    relative_vibration.setVisibility(View.VISIBLE);
                    text_voice.setVisibility(View.VISIBLE);
                    text_vibration.setVisibility(View.VISIBLE);
                    chatOptions.setNotifyBySoundAndVibrate(true);
                }
                break;
            case R.id.btn_voice:
                if (btn_voice.isSwitchOpen()) {
                    btn_voice.closeSwitch();
                    chatOptions.setNoticeBySound(false);
                } else {
                    btn_voice.openSwitch();
                    chatOptions.setNoticeBySound(true);
                }
                break;
            case R.id.btn_vibration:
                if (btn_vibration.isSwitchOpen()) {
                    btn_vibration.closeSwitch();
                    chatOptions.setNoticedByVibrate(false);
                } else {
                    btn_vibration.openSwitch();
                    chatOptions.setNoticedByVibrate(true);
                }
                break;
            case R.id.btn_outside:
                if (btn_outside.isSwitchOpen()) {
                    btn_outside.closeSwitch();
                    chatOptions.setUseSpeaker(false);
                } else {
                    btn_outside.openSwitch();
                    chatOptions.setUseSpeaker(true);
                }
                break;
            case R.id.btn_allow:
                if (btn_allow.isSwitchOpen()) {
                    btn_allow.closeSwitch();
                } else {
                    btn_allow.openSwitch();
                }
                break;

            case R.id.btn_logout:
                logout();
                break;
        }
    }


    void logout() {
        final ProgressDialog pd = new ProgressDialog(getActivity());
        String st = getResources().getString(R.string.dialog_out);
        pd.setMessage(st);
        pd.setCanceledOnTouchOutside(false);
        pd.show();


        EMChatManager.getInstance().logout(new EMCallBack() {
            @Override
            public void onSuccess() {
                pd.dismiss();
                getActivity().finish();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }

            @Override
            public void onError(int i, String s) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pd.dismiss();
                        Toast.makeText(getActivity(), "退出失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onProgress(int i, String s) {

            }
        });

    }
}